package algorithm.algexercise.sort;

/**
 * Created by chengxiaoqi1 on 2018/8/5.
 */

import edu.princeton.cs.algs4.Stopwatch;

import java.util.Arrays;

import static algorithm.Template.exch;
import static algorithm.Template.less;

/**
 * 插入排序
 */
public class Insertion {

    public static void main(String[] args) {
        Integer[] arr = new Integer[]{8,0,1,7,67,42};
        sort(arr);
    }

    public static void sort(Comparable[] a) {
        System.out.println(Arrays.asList(a));
        Stopwatch timer = new Stopwatch();
        int N = a.length;
        for (int i = 1; i < N; i ++) {
            for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
                exch(a, j, j-1);
                System.out.println("---" + Arrays.asList(a));
            }
            System.out.println(Arrays.asList(a));
        }
        System.err.println(timer.elapsedTime());
        System.out.println(Arrays.asList(a));
    }

}
